<?php

namespace app\common\model;

use tencentcloud\Live;
use think\helper\Str;
use helper\Files;


class FlowModel
{
  private $liveModel;

  //初始化
  public function __construct()
  {
    $this->liveModel = new Live();
  }



  /**
   * 获取统计信息 查询所有流的流量数据 
   * @return mixed
   */
  public function getList($DayTime)
  {
	$data  = ['rows' => [], 'total' => 0];
    try {
		$folwlist = $this->liveModel->execFun('DescribeStreamDayPlayInfoList',[
			 "DayTime"=>$DayTime
		]);  
      if ($folwlist['TotalNum'] > 0) {
		 $rows = $this->_formatList($folwlist['DataInfoList']);
        $data = ['rows' => $rows, 'total' => $folwlist['TotalNum']];
      }
    } catch (\Throwable $th) {
      $data['rows']    = [];
      $data['total']   = 0;
      $data['message'] = $th->getMessage();
    }
    return $data;
   
  }
 /**
   * 格式化列表
   * @param $list
   * @return mixed
   */
  public function _formatList($list)
  {
	  $rows = [];
        foreach ($list as $v) {
          $item = $v;
		  $room_id = explode('_',$v['StreamName'])[1];
          $item['meeting_name'] = D('live.Room')->where('room_id',$room_id)->value('room_title');
          $item['room_id'] = $room_id ;
		  $item['TotalFlux'] =   Files::byteFormat($v['TotalFlux']*1024*1024);
          $rows[] = $item;
        } 
    return $rows;
  }
  
  
  /**
   * 直播计费带宽和流量数据查询
   * @return mixed
   */
  public function broadband($param)
  {
    $data = $this->liveModel->execFun('DescribeBillBandwidthAndFluxList',$param);
    $data['SumFlux'] =  Files::byteFormat($data['SumFlux']*1024*1024);
    if (!empty($data['DataInfoList'])) {
      foreach ($data['DataInfoList'] as $v) {
        $v['Flux'] =   Files::byteFormat($v['Flux']*1024*1024);

      }
    }
    return $data;
  }
   /**
   * 查询按省份和运营商分组的播放数据
   * @return mixed
   */
  public function groupproispplay($param)
  {
	$data = $this->liveModel->execFun('DescribeGroupProIspPlayInfoList',$param);  
	 if (!empty($data['DataInfoList'])) {
		for($i = 0; $i < count($data['DataInfoList']); $i++) {
			$DetailInfoList = $data['DataInfoList'][$i]['DetailInfoList'];
			for($j = 0; $j < count($DetailInfoList); $j++) {
				$data['DataInfoList'][$i]['DetailInfoList'][$j]['Flux'] = Files::byteFormat($DetailInfoList[$j]['Flux']*1024*1024);
			}
			
			
		}
		 
		 
		
	}/* */
    return $data;
   
  }
  
   /**
   * 查询实时的域名维度下行播放数据
   * @return mixed
   */
  public function livedomainplay($param)
  {
	$data = $this->liveModel->execFun('DescribeLiveDomainPlayInfoList',$param);  
	
	$data['TotalFlux'] =  Files::byteFormat($data['TotalFlux']*1024*1024);
    return $data;
   
  }
  /**
   * 获取在线流的推流数据
   * @return mixed
   */
  public function livestreampush($param)
  {
	$data = $this->liveModel->execFun('DescribeLiveStreamPushInfoList',$param);  
	
    return $data;
   
  }

  /**
   * 查询分省份分运营商播放汇总数据
   * @return mixed
   */
  public function proispplay()
  {
    return $this->liveModel->execFun('DescribeProIspPlaySumInfoList',[
        "StartTime"=>date("Y-m-d H:i:s",strtotime("-1 day")),
        "EndTime"=>date('Y-m-d H:i:s'),
        "StatType"=>"Province",//统计的类型，可选值：”Province”，”Isp”，“CountryOrArea”。
    ]);
  }

  /**
   * 查询某段时间top n客户端ip汇总信息 
   * @return mixed
   */
  public function topclientip($param)
  {
	$data = $this->liveModel->execFun('DescribeTopClientIpSumInfoList',$param);  
	if (!empty($data['DataInfoList'])) {
		for($i = 0; $i < count($data['DataInfoList']); $i++) {
			$data['DataInfoList'][$i]['TotalFlux'] = Files::byteFormat($data['DataInfoList'][$i]['TotalFlux']*1024*1024);
		}
		
	}
    return $data;
   
  }
   /**
   * 查询某时间段top n的域名或流id信息 
   * @return mixed
   */
  public function visittop($param)
  {
	$data = $this->liveModel->execFun('DescribeVisitTopSumInfoList',$param);  
	if (!empty($data['DataInfoList'])) {
		for($i = 0; $i < count($data['DataInfoList']); $i++) {
			$data['DataInfoList'][$i]['TotalFlux'] = Files::byteFormat($data['DataInfoList'][$i]['TotalFlux']*1024*1024);
		}
	}
    return $data;
   
  }
  
  public function DescribeStreamPlayInfoList($startTime,$endTime,$streamName=''){
    $data = $this->liveModel->execFun('DescribeStreamPlayInfoList',[
      "StartTime"=>$startTime,
      "EndTime"=>$endTime,
      "PlayDomain"=>"play.live.180wx.cn",
      "StreamName"=>$streamName,
    ]);
    return $data;
  }
  
}